import wepy from 'wepy';
import util from './util';
import md5 from './md5';
import tip from './tip';
import {
	UDID,
  OPENID,
  USER_INFO
  } from '@/utils/constant';

// const TIMESTAMP = util.getCurrentTime()
// const SIGN = md5.hex_md5((TIMESTAMP + API_SECRET_KEY).toLowerCase())
const wxRequest = async(params = {}, url) => {
    // tip.loading();
  let openId = wepy.getStorageSync(OPENID);
  let userInfo = wepy.getStorageSync(USER_INFO);
  let data = params.query || {};
  for (var key in data) {
    if (data[key] == 'undefined' || data[key] == undefined || data[key] == 'null') {
      data[key] = null
    }
       // console.log(key+" "+data[key])
  }
	  let header = params.header || {'Content-Type': 'application/json'};
    // data.sign = SIGN;
    // data.time = TIMESTAMP;
  if (userInfo != '' && userInfo != null && userInfo != undefined) {
    header.userId = userInfo.userId;
    header.token = userInfo.token
    // header.token = 'eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiLmtYvor5XpnLgiLCJhdWRpZW5jZSI6ImUxMGFkYzM5NDliYTU5YWJiZTU2ZTA1N2YyMGY4ODNlIiwidXNlcl9pZCI6NzA0NjQsImNyZWF0ZWQiOjE2NDg3MTc3NjM2MTcsImV4cCI6MTY1MTMwOTc2MywidXNlcl9ncm91cF9pZCI6NDE5OTYsInBsYXRmb3JtIjpudWxsfQ.-3hpRz2zs6OD6NBTvWG1cwIvlwIIoZjg-6bzRn8vTXHKZ_cTauZhfV2PCtv-umpiuJ7Lw8mFhhO1g-YkbRTvkQ'
  }
  if (openId != '' && openId != null && openId != undefined) {
    header.UDID = openId;
  }
  header.version = '7.6.7';
  header.platform = 'wechat';
  let res = await wepy.request({
    url: params.url || url,
    method: params.method || 'GET',
    data: data,
    header: header
  });
  tip.loaded();
  // 新用户跳转H5
  if (res.data.customPushUrl != '' && res.data.customPushUrl != null && res.data.customPushUrl != undefined && res.data.code == 0) {
    // 跳转web
    let web = res.data.customPushUrl
    if (header.customPushUrl != '' && header.customPushUrl != null && header.customPushUrl != undefined) {
      wepy.redirectTo({
        url: '/pages/home/webview?url=' + encodeURIComponent(web)
      })
    } else {
      wepy.navigateTo({
        url: '/pages/home/webview?url=' + encodeURIComponent(web)
      })
    }
    return res
  }
  if (res != null && res.data != null && res.data.code == 200) {
    wepy.navigateTo({ url: '/pages/authorize' });
	   return res
  } else if (res != null && res.data != null && res.data.code == 201) {
    wepy.navigateTo({ url: '/packageA/pages/person/setting/bangding' });
	   return res
  } else if (res != null && res.statusCode == 901) {
    wx: wx.showToast({
      title: '系统访问量过大，请稍后再试',
      icon: 'none'
    })
	   return res
  } else if (res != null && res.data != null && res.data.code == 100) {
    wepy.navigateTo({ url: '/packageA/pages/person/setting/authorizeBd' });
	   return res
  } else if (res != null && res.data != null && res.data.code == 500) {
    let errorStr = '网络出错'
    if (res.data.msg) {
      errorStr = res.data.msg
    }
    if (errorStr.length > 38) {
      wx.showModal({
        title: '错误提示',
        content: errorStr,
        showCancel: false, // 是否显示取消按钮
        success: function(res) {
          if (res.confirm) {
              // 确定按钮
              // console.log(id)
          } else if (res.cancel) {
          }
        }
      })
    } else {
      wx: wx.showToast({
        title: errorStr,
        icon: 'none'
      })
    }
	   return res
  } else {
    return res;
  }
};
module.exports = {
  wxRequest
}
